package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;

import com.google.gson.Gson;
import com.zretc.emptraining.dao.EmpDao;
import com.zretc.emptraining.dao.UsersDao;
import com.zretc.emptraining.daoimpl.EmpDaoImpl;
import com.zretc.emptraining.daoimpl.UsersDaoImpl;
import com.zretc.emptraining.entity.AjaxResponse;
import com.zretc.emptraining.entity.Dept;
import com.zretc.emptraining.entity.Emp;
import com.zretc.emptraining.entity.Teach;
import com.zretc.emptraining.entity.Users;
import com.zretc.emptraining.service.UsersService;

import jdk.nashorn.internal.runtime.linker.LinkerCallSite;

/**
 * Servlet implementation class Login
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 实例化接口
	UsersDao usersDao = new UsersDaoImpl();
	EmpDao empDao = new EmpDaoImpl();

	/**
	 * Default constructor.
	 */
	public LoginServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码 防止中文乱码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String op1 = "";
		if (request.getParameter("op1") != null) {
			op1 = request.getParameter("op1");
		}
		// 登录
		if (op1.equals("")) {
			doLogin(request, response);
		}
		// 退出登录
		if ("logout".equals(op1)) {
			doLogout(request, response);
		}
	}
/**
 * 退出登录
 * @param request
 * @param response
 * @throws IOException
 */
	private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getSession().invalidate();
		String msg = "成功退出！";
		int code = 1;
		AjaxResponse ar = new AjaxResponse(msg, code);
		System.out.println(ar);
		// 将ar转换为JSON格式的字符串
		Gson gson = new Gson();
		String msgjson = gson.toJson(ar);
		System.out.println(msgjson);
		// 响应
		PrintWriter out = response.getWriter();
		out.print(msgjson);
		// 关闭
		out.close();
	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 用户身份
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		// 是否记住密码
		String rem = "";
		if (request.getParameter("rem") != null) {
			rem = request.getParameter("rem");
		}
		// 先获取用户名和密码
		String username = "";
		if (request.getParameter("username") != null) {
			username = request.getParameter("username");
		}
		String userpwd = "";
		if (request.getParameter("userpwd") != null) {
			userpwd = request.getParameter("userpwd");
		}

		// 账号密码是否正确
		boolean flag = usersDao.doLogin(username, userpwd);

		String msg = "账号或密码有误，请重新输入！";
		int code = 0;
		if (flag) {
			// 用户选择了记住密码
			if (rem.equals("true")) {
				// 记住密码
				// 创建cookie，并设置其过期时间，存储到本地
				Cookie cookie1 = new Cookie("username", username);
				cookie1.setMaxAge(60 * 60);// 1h
				Cookie cookie2 = new Cookie("userpwd", userpwd);
				cookie2.setMaxAge(60 * 60);
				// 将cookie的信息存储在本地: addCookie
				response.addCookie(cookie1);
				response.addCookie(cookie2);
			}
			// 将用户登录信息存3储在session中
			request.getSession().setAttribute("username", username);

			
			if ("emp".equals(op)) {
				System.out.println(op);
				// 员工
				// 通过手机号获得员工编号
				int empNo = usersDao.getUsersEmpNo(username).getEmpNo();
				// 通过员工编号获得员工职位
				String job = empDao.getEmpsByNo(empNo).getEmpJob();
				if ("主管".equals(job)) {
					msg = "主管，登录成功！";
					code = 1;
					//获取部门号
					List<Dept> list=usersDao.getDeptNo(empNo);
					int deptNo=list.get(0).getDeptNo();
					System.out.println(deptNo);
					request.getSession().setAttribute("deptNo", deptNo);
					request.getSession().setAttribute("empNo", empNo);
					System.out.println("部门号"+deptNo);
				} else if ("教员".equals(job)) {
					msg = "教员，登录成功！";
					code = 2;
					//将教员号也存储
					List<Teach> list=usersDao.getTeachNo(username);
					int teachNo=list.get(0).getTeacherNo();
					request.getSession().setAttribute("teachNo", teachNo);
					request.getSession().setAttribute("empNo", empNo);
					System.out.println("教员号"+teachNo);
				} else {
					msg = "员工，登录成功！";
					code = 3;
					//将员工号也存储
					request.getSession().setAttribute("empNo", empNo);
					System.out.println("员工号"+empNo);
				}
				
				System.out.println(empNo);
			}
			if ("admin".equals(op)) {
				msg = "管理员，登录成功！";
				code = 4;
			}
		}
		// 账号密码错误
		AjaxResponse ar = new AjaxResponse(msg, code);
		
		// 将ar转换为JSON格式的字符串
		Gson gson = new Gson();
		String msgjson = gson.toJson(ar);
		System.out.println(msgjson);
		// 响应
		PrintWriter out = response.getWriter();
		out.print(msgjson);
		// 关闭
		out.close();
	}

	public void Gson(String keyword, HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		String toJson = gson.toJson(keyword);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(toJson);
		printWriter.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
